package org.alis.smallcc.alis.tools.lock.distributed;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock;

/**
 * zookeeper 读写锁
 * 
 * @author: luochuan     
 * @date: 2020年10月29日 下午6:30:57
 */
public class ZookeeperReadWriteLock implements ReadWriteLock{
	private final String path;
	private final CuratorFramework client;
	private final InterProcessReadWriteLock mutex;
	private final ThreadPoolExecutor executor;
	public ZookeeperReadWriteLock(CuratorFramework client,String path,ThreadPoolExecutor executor) {
		this.client =client;
		this.path = path;
		this.mutex = new InterProcessReadWriteLock(client, path);
		this.executor =executor;
	}
	
	@Override
	public Lock readLock() {
		return new ZookeeperLock(client, path,mutex.readLock(),executor);
	}

	@Override
	public Lock writeLock() {
		return new ZookeeperLock(client, path,mutex.writeLock(),executor);
	}

}
